#include "GUI/GameOverScreen.h"


namespace TD_GUI {

	/************************************************************************/
	/* ctors and dtors                                                      */
	/************************************************************************/
	GameOverScreen::GameOverScreen(bool gameWon) {
		m_bowserIndex = 0;
		m_pFadescreen = new Fadescreen();
		m_pFadescreen->startFadeIn(10/frameRate);
		
		if(!gameWon) {
			m_pImage = TD_Resources::ResourceManager::loadImage("Gameovers/GameOver Lost.png");
			m_pGameoverMusic = TD_Resources::ResourceManager::loadMusic("SFX/music/lose.mp3");
			m_pGameoverMusic->playMusic(false);
		} else {
			m_pImage = TD_Resources::ResourceManager::loadImage("Gameovers/win.png");
		}

		shared_ptr<SDLImage> optionsImage = ResourceManager::loadImage("Gameovers/GameoverOptions.png", Color::PINK);
		Coord2D optionsPos;
		optionsPos.x = screenWidth/2 - optionsImage->getBounds().x/2;
		optionsPos.y = 550;
		m_pGameOverOptions = new Tile(optionsPos, optionsImage->getBounds(), imgTile, optionsImage);
		
		shared_ptr<SDLImage> bowserImage = TD_Resources::ResourceManager::loadImage("GUI/Icons/bowser icon.png", Color::PINK);
		optionsPos.x += 10;
		m_pBowserIcon = new Tile(optionsPos, bowserImage->getBounds(), imgTile, bowserImage);

		
	}

	GameOverScreen::~GameOverScreen() {
		Utils::safeDelete<Tile>(m_pBowserIcon);
		Utils::safeDelete<Tile>(m_pGameOverOptions);
		Utils::safeDelete<Fadescreen>(m_pFadescreen);
	}

	void GameOverScreen::moveDownBowserIcon() {
		if(m_pBowserIcon != NULL && m_bowserIndex < 1) {
			m_pBowserIcon->setPosition(m_pBowserIcon->getPosition() + Coord2D(0, 100));
			++m_bowserIndex;
		}
	}

	void GameOverScreen::moveUpBowserIcon() {
		if(m_pBowserIcon != NULL && m_bowserIndex > 0) {
			m_pBowserIcon->setPosition(m_pBowserIcon->getPosition() - Coord2D(0, 100));
			--m_bowserIndex;
		}
	}


	interaction_t GameOverScreen::processEvent(const SDL_Event &event) {
		interaction_t interaction;
		interaction.type = noInteraction;

		if( event.type == SDL_KEYDOWN ) { 
			//Set the proper message surface
			switch( event.key.keysym.sym ) {
				case SDLK_RETURN:
					if(m_bowserIndex == 0) {
						interaction.value = m_bowserIndex;
						interaction.type = restartGame;
						if(m_pFadescreen->isFadingIn())
							m_pFadescreen->startFadeOut(10/frameRate);
					} else if(m_bowserIndex == 1) {
						interaction.type = gameExit;
					}
					break;
				case SDLK_UP:
					moveUpBowserIcon();
				break;

				case SDLK_DOWN:
					moveDownBowserIcon();
				break;

				case SDLK_ESCAPE:
					interaction.type = gameExit;
				break;
			}
		}
		
		return(interaction);
	}

	void GameOverScreen::render(SDL_Surface *const g_destSurface, unsigned int deltaTime) {
		BasePanel::render(g_destSurface, deltaTime);
		m_pGameOverOptions->render(g_destSurface, deltaTime);
		m_pBowserIcon->render(g_destSurface, deltaTime);
	}
}